package com.corpgovernment.organization.util;

import com.corpgovernment.common.base.CommonException;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.utils.DateUtil;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Component
public class ParamsValidator {

    public void validParam(Errors errors) {
        if (errors.hasErrors()) {
            List<ObjectError> error = errors.getAllErrors();
            StringBuilder validMsgBuilder = new StringBuilder();
            for (ObjectError objectError : error) {
                validMsgBuilder.append("[").append(objectError.getDefaultMessage()).append("]");
            }
            throw new CorpBusinessException(OrganizationResponseCodeEnum.CUSTOM_CONTENT, validMsgBuilder.toString());
        }
    }

    public void validNotNullParam(Object obj, String msg) {
        if (obj == null) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.CUSTOM_CONTENT, msg);
        } else if (obj instanceof String && StringUtils.isBlank(String.valueOf(obj))) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.CUSTOM_CONTENT, msg);
        }
    }

    /**
     * 校验时间区间，过滤为空的时间段
     */
    public void validTimeIntervals(List<List<String>> timeIntervals) {
        Iterator<List<String>> it = timeIntervals.iterator();
        while (it.hasNext()) {
            List<String> timeInterval = it.next();
            if (CollectionUtils.isEmpty(timeInterval)) {
                it.remove();
                continue;
            }
            PreCheckUtil.checkArgument(timeInterval.size() == 2, "预算时间不正确");
            PreCheckUtil.checkNotEmpty(timeInterval.get(0), "开始月份不能为空");
            PreCheckUtil.checkNotEmpty(timeInterval.get(1), "结束月份不能为空");
        }
        //校验时间两两之间不重叠
        for (int i = 0; i < timeIntervals.size(); i++) {
            for (int j = i + 1; j < timeIntervals.size(); j++) {
                PreCheckUtil.checkArgument(!converInternal(timeIntervals.get(i), timeIntervals.get(j)), String.format("[%s]与[%s]时间重复，请检查",
                        String.format("%s至%s", timeIntervals.get(i).get(0), timeIntervals.get(i).get(1)),
                        String.format("%s至%s", timeIntervals.get(j).get(0), timeIntervals.get(j).get(1))));
            }
        }
    }

    /**
     * 校验时间区间有重叠
     */
    private boolean converInternal(List<String> interval1, List<String> interval2) {
        Date start1 = DateUtil.stringToDate(interval1.get(0), DateUtil.DF_YM);
        Date end1 = DateUtil.stringToDate(interval1.get(1), DateUtil.DF_YM);
        Date start2 = DateUtil.stringToDate(interval2.get(0), DateUtil.DF_YM);
        Date end2 = DateUtil.stringToDate(interval2.get(1), DateUtil.DF_YM);
        return (start2.compareTo(start1) >= 0 && start2.compareTo(end1) <= 0) ||
                (end2.compareTo(start1) >= 0 && end2.compareTo(end1) <= 0) ||
                (start2.compareTo(start1) <= 0 && end2.compareTo(end1) >= 0);
    }
}